﻿using CKS.Core.Framework;
using CKS.PLC.Action.Subscription.OP080;
using FreeSql;
using Infrastructure;
using Microsoft.AspNetCore.DataProtection.KeyManagement;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


using ZR.Model.Models.ProductionControl;
using ZR.Model.System;
using ZR.Service.IService.ProductionControl;
using ZR.ServiceCore.Services;

namespace CKS.PLC.Action.Subscription.OP71
{
    /// <summary>
    /// 动作编号 5/6
    /// 实时采集9点温度数据
    /// </summary>
    public class DB1200_Work05Action : PLCAction
    {
       /// <summary>
       /// 实时采集
       /// </summary>
       /// <param name="_value"></param>
        public DB1200_Work05Action(DecodeValue _value) : base(_value)
        {
          
        }
        public override void Execute()
        {
            
            var result = Value.Value.ToInt();

            
           
            //判断上次采集时间与当前时间的时间差。
            //从数据库查询最后采集时间
            //从数据库获取采集间隔
            CksConfig internalConfig = conn.Select<CksConfig>().Where(f => f.ConfigKey == "sys.brazing.internal").First();
            if(this.Value.LastTime == App.MinTime)
            {
                this.Value.LastTime = DateTime.Now.AddDays(-1);
            }

            

            var ts = DateTime.Now - this.Value.LastTime;
            if(ts.TotalSeconds+0.05 > internalConfig.ConfigValue.ToDouble() )
            {
                this.Value.LastTime = DateTime.Now;

                var TempFrontUp = Value.GetLinkData(2).ToDecimal();
                var TempFrontMid = Value.GetLinkData(3).ToDecimal();
                var TempFrontDown = Value.GetLinkData(4).ToDecimal();
                var TempMidUp = Value.GetLinkData(5).ToDecimal();
                var TempCenter = Value.GetLinkData(1).ToDecimal();
                var TempMidDown = Value.GetLinkData(6).ToDecimal();
                var TempBackUp = Value.GetLinkData(7).ToDecimal();
                var TempBackMid = Value.GetLinkData(8).ToDecimal();
                var TempBackDown = Value.GetLinkData(9).ToDecimal();

                int dataActionId = Value.Position.ActionId == 5 ? 1201 : 1203; //只有5和6，5代表1号钎焊炉的实时采集，对应配置的actionid为1

                var FurnaceNo = Value.GetLinkData(dataActionId, 7).ToInt();//得到当前炉号
                var VacuumDegree = Value.GetLinkData(dataActionId, 4).ToDecimal();

                //保存采集的温度信息
                var locationId = Value.Position.LocationId;
                var model = db.Queryable<CksBrazingRecord>()
                    .Where(a=>a.LocationId == locationId).OrderByDescending(a => a.Id).First();
                if (model != null && model.Status == 1)
                {
                    //当前正在加热产品，保存此温度信息 CksHeatTreatmentDetails
                    CksBrazingDetails details = new CksBrazingDetails() { 
                        BatchNo = model.BatchNo,
                        FurnaceNo = model.FurnaceNo,
                        MaterialRackNo = model.MaterialRackNo,
                        TempBackDown = TempBackDown,
                        TempBackMid = TempBackMid,
                        TempBackUp = TempBackUp,
                        TempCenter = TempCenter,
                        TempFrontDown = TempFrontDown,
                        TempFrontMid = TempFrontMid,
                        TempFrontUp = TempFrontUp,
                        TempMidDown = TempMidDown,
                        TempMidUp = TempFrontUp,
                        VacuumDegree = VacuumDegree,
                    };
                    conn.Insert(details).ExecuteAffrows(); //保存温度记录
                }

                //将温度信息推送到实时曲线
                //var job = InternalApp.JobProvider.GetService<PushJob>();
                //job.AddDegree(degree1,degree2);
                
            }
        }

        
    }
}
